package com.leetcode.LC_MED;

public class LC152 {
    public int maxProduct(int[] nums) {
//https://leetcode-cn.com/problems/maximum-product-subarray/solution/hua-jie-suan-fa-152-cheng-ji-zui-da-zi-xu-lie-by-g/
        int max = 1, min = 1, rs = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {

            if (nums[i] < 0) {
                int tmp = max;
                max = min;
                min = tmp;
            }

            max = Math.max(max * nums[i], nums[i]);
            min = Math.min(min * nums[i], nums[i]);

            rs = Math.max(rs, max);
        }
        return rs;
    }
}
